package com.zuowei.ycy.user.ui.mine.balance;

import android.text.TextUtils;
import android.view.View;
import android.widget.RadioGroup;

import com.and.yzy.frame.util.AppManger;
import com.and.yzy.frame.util.RetrofitUtils;
import com.and.yzy.frame.wxpay.WxPay;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.alipay.AliPay;
import com.zuowei.ycy.user.config.AppConfig;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.http.Member;
import com.zuowei.ycy.ui.BaseToolbarAty;
import com.zuowei.ycy.user.util.AppJsonUtil;

import butterknife.Bind;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/4/26.
 */
public class RechargePayAty extends BaseToolbarAty {

    @Bind(R.id.radiogroup_type)
    RadioGroup radiogroupType;
    private String money = "";
    private int type = 1;

    private String order_sn;

    @Override
    public int getLayoutId() {
        return R.layout.recharge_pay_aty;
    }

    @Override
    public void initData() {
        mToolbar.setTitle("充值");
        money = getIntent().getStringExtra("money");

        radiogroupType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radiobtn_pay_zfb:
                        type = 2;
                        break;
                    case R.id.radiobtn_pay_wx:
                        type = 1;
                        break;
                }
            }
        });

    }

    private boolean isThreedPay;

    @Override
    protected void onResume() {
        super.onResume();
        if (isThreedPay) {
            showLoadingDialog("");
            doHttp(RetrofitUtils.createApi(Member.class).findStatus(order_sn), 1);
        }
        isThreedPay = false;
    }


    @Override
    public void requestData() {

    }

    @OnClick({R.id.fbtn_confirm})
    @Override
    public void btnClick(View view) {
        super.btnClick(view);
        switch (view.getId()) {
            case R.id.fbtn_confirm:
                if (TextUtils.isEmpty(order_sn)) {
                    showLoadingDialog("");
                    doHttp(RetrofitUtils.createApi(Member.class).amountTransfer(UserManger.getM_id(), money, type + ""), 0);
                } else {
                    switch (type) {
                        case 1: //微信
                            isThreedPay = true;
                            WxPay wxPay = new WxPay(this);
                            wxPay.pay("余额充值",money,order_sn, AppConfig.RECHARGE_WX_NOTIFY_URL);
                            break;
                        case 2: //支付宝
                            isThreedPay = true;
                            AliPay aliPay = new AliPay("余额充值","支付",money,order_sn);
                            aliPay.setNotifyUrl(AppConfig.RECHARGE_ZFB_NOTIFY_URL);
                            aliPay.pay();
                            break;
                    }
                }
                break;
        }
    }


    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        if (what == 0) { //生成订单
            order_sn = AppJsonUtil.getString(result, "order_sn");
            switch (type) {
                case 1: //微信
                    isThreedPay = true;
                    WxPay wxPay = new WxPay(this);
                    wxPay.pay("余额充值",money,order_sn, AppConfig.RECHARGE_WX_NOTIFY_URL);
                    break;
                case 2: //支付宝
                    isThreedPay = true;
                    AliPay aliPay = new AliPay("余额充值","支付",money,order_sn);
                    aliPay.setNotifyUrl(AppConfig.RECHARGE_ZFB_NOTIFY_URL);
                    aliPay.pay();
                    break;
            }
        }else if (what==1){//订单支付状态
            if (AppJsonUtil.getString(result,"status").equals("1")){
                showToast("支付成功");
                AppManger.getInstance().killActivity(RechargeOneAty.class);
                finish();
            }else {
                showToast("支付失败");
            }
        }
        super.onSuccess(result, call, response, what);
    }
}
